package com.dtagri.fjwlw.util;

import org.MediaPlayer.PlayM4.Player;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.SurfaceHolder.Callback;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Toast;

import com.android.nvssdklib.NvssdkAPI;
import com.dtagri.fjwlw.R;
import com.dtagri.fjwlw.bean.TjNvr;
import com.dtagri.fjwlw.bean.TjNvrChanel;
import com.dtagri.fjwlw.bean.tdvideo.VideoInfo;
import com.dtagri.fjwlw.bean.tdvideo.cls_Channel;
import com.dtagri.fjwlw.bean.tdvideo.cls_Host;
import com.dtagri.fjwlw.fragment.VideoCenterFragment;
//import com.dtagri.fjwlw.ui.Show_Video_View;
import com.dtagri.fjwlw.ui.VView;
import com.dtagri.fjwlw.ui.VideoCenterActivity;
import com.dtagri.fjwlw.ui.tdvideo.CLS_Channel;
import com.dtagri.fjwlw.ui.tdvideo.CLS_VideoViewController;



/**
 * 天地伟业视频帮助类
 * 
 * 
 */
public class TDVideoHelper  implements Callback {


	private static boolean sblInitSDK = false; //记录是否初始化SDK
	private final static String TAG = "TDVideoHelper";
	public CLS_VideoViewController m_cls_VideoViewController;
	private int m_iPort = -1;
	private Context context;
	private VView vview;
	private cls_Host cls_SelectHost;
	private cls_Channel cls_SelectChannel;
	public  MyHandler msgHandler;
	private int width,height;
	private boolean m_bFirstCreate = true; // 标示当前第一次打开
	// 当前系统分辨率
	private int m_iWindowWidth, m_iWindowHeight, m_BodyHeight;
	private RelativeLayout m_LayoutBody;
	// 记录视频窗口普通和全屏是的宽和高
	private int m_iNormalVideoWidth, m_iNormalVideoHeight, m_iFullVideoWidth,
	m_iFullVideoHeight;


	public TDVideoHelper(Context context,VView view,int Width,int Height,RelativeLayout m_LayoutBody) {

		this.context=context;
		this.vview=view;
		this.width=Width;
		this.height=Height;
		this.m_LayoutBody=m_LayoutBody;
		// 得到当前线程的Looper实例，由于当前线程是UI线程也可以通过Looper.getMainLooper()得到
		Looper looper = Looper.myLooper();

		// 此处甚至可以不需要设置Looper，因为 Handler默认就使用当前线程的Looper
		msgHandler = new MyHandler(looper);
		LoadLib();
		InitVaraible();
		SuitScreen();
		InitSDK();
	}
	public TDVideoHelper(){
		// 得到当前线程的Looper实例，由于当前线程是UI线程也可以通过Looper.getMainLooper()得到
		Looper looper = Looper.myLooper();

		// 此处甚至可以不需要设置Looper，因为 Handler默认就使用当前线程的Looper
		msgHandler = new MyHandler(looper);
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {


		Log.i(TAG, "surface is created" + m_iPort);
		if (-1 == m_iPort) {
			return;
		}
		Surface surface = holder.getSurface();
		if (true == surface.isValid()) {
			if (false == Player.getInstance()
					.setVideoWindow(m_iPort, 0, holder)) {
				Log.e(TAG, "Player setVideoWindow failed!");
			}
		}


	}
	/**
	 * 
	 * 初始化视频相关组件
	 */
	public  void InitVaraible() {

		m_cls_VideoViewController = CLS_VideoViewController.GetInstance();//new CLS_VideoViewController();
		m_cls_VideoViewController.Registe(this);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.i(TAG, "Player setVideoWindow release!" + m_iPort);
		if (-1 == m_iPort) {
			return;
		}
		if (true == holder.getSurface().isValid()) {
			if (false == Player.getInstance().setVideoWindow(m_iPort, 0, null)) {
				Log.e(TAG, "Player setVideoWindow failed!");
			}
		}
	}

	/**
	 * 
	 * 加载播放视频所需的库
	 */
	public void LoadLib() {
		//	    	String strLibPath = android.os.Environment.getDataDirectory().getAbsolutePath()
		//			+ "/data/com.easymobile.show/lib/libnvssdk.so";
		System.out.println("初始化设备~~~~~~~~~~~~");
		String strLibPath = android.os.Environment.getDataDirectory().getAbsolutePath()
				+ "/data/com.dtagri.fjwlw/lib/libnvssdk.so";
		Log.i("register","strLibPath = "+strLibPath);

		System.loadLibrary("NvssdkAPI");
		NvssdkAPI.GetInstance().InitalNvssdk(strLibPath);
	}
	/**
	 * 初始化SDK
	 * 
	 */
	public  void InitSDK() {
		if (!sblInitSDK) // 初始化SDK的函数
		{
			NvssdkAPI.GetInstance().NetClient_Startup(3000, 3000);
			sblInitSDK = true;
		}

		NvssdkAPI.GetInstance().NetClient_SetNotifyFunction(
				m_cls_VideoViewController);
	}
	/**
	 * 
	 * 绑定视频数据
	 */
	public void bindingVideoParam(TjNvr entity,TjNvrChanel entity2) {

		// entity.setVideoIp("192.168.16.224");
		cls_SelectHost = new cls_Host();
		//		cls_SelectHost.setHostID(entity.getHostID()); // 11
		//		cls_SelectHost.setHostName(entity.getVideoIp());
		//		cls_SelectHost.setConnectTypeID(0);
		//		cls_SelectHost.setChannelNumber(1);
		cls_SelectHost.setConnectIP(entity.getNvrwanip());
		cls_SelectHost.setUserName(entity.getNvrusername());
		cls_SelectHost.setPassword(entity.getNvruserpwd());
		cls_SelectHost.setConnectPort(Integer.parseInt(entity.getNvrdataport()));
		//		cls_SelectHost.setDeviceID("");

		cls_SelectChannel = new cls_Channel();
		cls_SelectChannel.setChannelName("channel" + entity2.getSschanelnum());
		//		cls_SelectChannel.setProtocol(String.valueOf(entity.getVideoBaud()));
		//		cls_SelectChannel.setChannelID(1);
		cls_SelectChannel.setChannelNo(entity2.getSschanelnum());
		//		cls_SelectChannel.setComNumber(entity.getVideoComNum());
		//		cls_SelectChannel.setControlAddress(entity.getVideoAddress());
		cls_SelectChannel.setControlStep(entity2.getSsstep());
		//		cls_SelectChannel.setHostID(entity.getHostID());
		//		cls_SelectChannel.setRecommendID(111);
		//		cls_SelectChannel.setRecTime(111);
		//		cls_SelectChannel.setPTZType(entity.getVideoProtocol());
		cls_SelectChannel.SetStreamNo(entity2.getSsstream());
		m_cls_VideoViewController.LogonSelctedChannel(cls_SelectHost, cls_SelectChannel, vview);


	}


	/**
	 * 
	 * 视频显示适应窗口
	 */
	public  void SuitScreen() {
		//从外部获取手机屏幕分辨率
		m_iWindowWidth = width;
		m_iWindowHeight = height;
		m_iFullVideoWidth = m_iWindowWidth;

		// -------------------------------------------
		//竖屏时视频大小设置
		if(VideoCenterFragment.state==0){
			m_BodyHeight = m_iWindowHeight/2+10;
			//			Toast.makeText(context, "竖屏", 500).show();
		}
		//横屏时视频大小设置（由于屏幕长宽是 从外面传进来的，所以横屏之后长宽大小对调）
		else {
			m_BodyHeight = m_iWindowWidth-80;
			m_iFullVideoWidth=m_iWindowHeight;

		}

		// 实际中间列表的高度

		m_iFullVideoHeight = m_BodyHeight;
		m_iNormalVideoWidth = (int) m_iWindowWidth / 2 - 1;
		m_iNormalVideoHeight = (int) m_BodyHeight / 2;

		m_cls_VideoViewController.SetViewSize(m_iWindowWidth, m_iWindowHeight,
				m_BodyHeight, m_iNormalVideoWidth, m_iNormalVideoHeight);


		vview.SetCurrentScreenSize(m_iFullVideoWidth,
				m_BodyHeight);


		return;

	}

	/**
	 * 
	 * 控制视频播放、屏幕旋转
	 * @author Administrator
	 *
	 */
	public class MyHandler extends Handler {
		int icount = 0;

		public MyHandler(Looper looper) {

			super(looper);

		}

		@Override
		public void handleMessage(Message msg) {
			int iswitch = msg.arg1;

			switch (iswitch) {
			case -1:
				TToast.showToast("加载失败");

				break;
			case 1: // 登录成功开始接受视频
				String strDeviceName = m_cls_VideoViewController
				.GetSelectHostName();
				String strChannelName = m_cls_VideoViewController
						.GetSelectChannelName();

				break;
			case 0: // 开始播放视频

				if (cls_SelectHost == null || cls_SelectChannel == null) { // 请先添加设备信息！
					Toast.makeText(context,
							"设备添加出错", Toast.LENGTH_SHORT)
							.show();
					return;
				}
				if (!m_cls_VideoViewController.HasRev()) { // 请登录设备
					Toast.makeText(context,
							"设备登录失败", Toast.LENGTH_SHORT)
							.show();
					return;
				}

				m_cls_VideoViewController.PlaySelctedChannel();
				break;

			case 2:
				String strGPSInfo = m_cls_VideoViewController.getStrGPSInfo();
				//				Toast.makeText(context, strGPSInfo, 500).show();
				break;
			case 88://重新适应屏幕大小
				//				Toast.makeText(context, "视频放大测试-->横屏", 500).show();
				SuitScreen();
				break;
			default:
				//				Toast.makeText(context, "未知的返回信息"+iswitch, 500).show();
				break;
			}
		}

	}



}
